﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.Services;

using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

namespace SLIITPro.Service
{
    /// <summary>
    /// Summary description for BusinessServices
    /// </summary>
    [WebService(Namespace = "http://SLIITPro.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class BusinessServices : System.Web.Services.WebService
    {
        //private SqlConnection _conn;
        [WebMethod]
        public Business GetBusiness(int bId)
        {
            BusinessItemExtension itemExtension = new BusinessItemExtension();
            BusinessItem item = new BusinessItem(itemExtension.SelectBusiness(bId).Rows[0]);
            Business business = new Business
                                    {
                                        BId = item.BId,
                                        BCity = item.BCity,
                                        BCountry = item.BCountry,
                                        BEmail = item.BEmail,
                                        BName = item.BName,
                                        BPhone = item.BPhone,
                                        BStreet = item.BStreet,
                                        BWeb = item.BWeb
                                    };
            return business;
        }

        [WebMethod]
        public List<Business> GetAllBusiness(string type, string location, string searchTerm)
        {
            if (string.IsNullOrEmpty(searchTerm)) return null;
            var businessItemExtension = new Data.Search();
            List<BusinessItem> list = businessItemExtension.GetSearchResults(searchTerm, type, location);
            return list.Select(businessItem => new Business
                                                   {
                                                       BId = businessItem.BId,
                                                       BCity = businessItem.BCity,
                                                       BCountry = businessItem.BCountry,
                                                       BEmail = businessItem.BEmail,
                                                       BName = businessItem.BName,
                                                       BPhone = businessItem.BPhone,
                                                       BStreet = businessItem.BStreet,
                                                       BWeb = businessItem.BWeb
                                                   }).ToList();
        }


        [WebMethod]
        public User IsAuthentcateUser(string username, string password)
        {

            UserExtension userExtension = new UserExtension();
            DataRow user;
            user = userExtension.AuthenticateUser(username, password);
            Users users = new Users(user);
            if (user != null)
            {
                User authUser = new User();
                authUser.UserId = users.UserId;
                authUser.FirstName = users.FirstName;
                authUser.LastName = users.LastName;

                return authUser;
            }

            return null;
        }

        [WebMethod]
        public void SetWishList(int userId, int businessId)
        {
            WishListExtension wishListExtension = new WishListExtension();
            wishListExtension.SetWishList(userId, businessId);
        }

        [WebMethod]
        public bool IsWish(int userId, int businessId)
        {
            WishListExtension wishListExtension = new WishListExtension();
            return wishListExtension.IsWish(userId, businessId);
        }

        [WebMethod]
        public List<Business> SelectWishBusinessList(int userId)
        {
            WishListExtension wishListExtension = new WishListExtension();
            List<BusinessItem> list = wishListExtension.SelectWishedBusinesses(userId);
            return list.Select(businessItem => new Business
                                                   {
                                                       BId = businessItem.BId,
                                                       BCity = businessItem.BCity,
                                                       BCountry = businessItem.BCountry,
                                                       BEmail = businessItem.BEmail,
                                                       BName = businessItem.BName,
                                                       BPhone = businessItem.BPhone,
                                                       BStreet = businessItem.BStreet,
                                                       BWeb = businessItem.BWeb
                                                   }).ToList();
        }

        [WebMethod]
        public void RemoveFromWishList(int userId, int businessId)
        {
            WishListExtension wishListExtension = new WishListExtension();
            wishListExtension.RemoveFromWishList(userId, businessId);
        }


        [WebMethod]
        public cellInfo SelectCellInfo(int cellId, int lac, int mnc, int mcc)
        {
            CellIdExtension cellIdExtension = new CellIdExtension();
            var row = cellIdExtension.GetLatLang(cellId, lac, mnc, mcc);
            cellInfo cell = new cellInfo { lat = row[1].ToString(), lng = row[2].ToString() };
            return cell;
        }
    }

    public class cellInfo
    {
        public string lng { get; set; }
        public string lat { get; set; }
    }

}
